<template>
  <div class="home">
    <van-nav-bar class="page-nav-bar" @click-left="$router.back()" fixed>
      <van-button
        slot="title"
        type="info"
        icon="search"
        class="searchBtn"
        size="small"
        round
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- tab 切换栏 -->
    <van-tabs v-model="active" class="exchange_tab" animated swipeable>
      <van-tab :title="item.name" v-for="item in channels" :key="item.id">
        <!-- 局部组件 -->
        <articleList :channel="item"></articleList
      ></van-tab>

      <div slot="nav-right" class="placeHolder"></div>
      <div slot="nav-right" class="hamburg" @click="editPopupShow = true">
        <i slot="icon" class="iconfont icongengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="editPopupShow"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <!-- 频道编辑组件 -->
      <channelEditVue
        :myChannels="this.channels"
        :active="active"
        @changeChannel="changeChannel"
        @changeActive="changeActive"
      ></channelEditVue>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
// 导入文章列表组件
import ArticleList from './components/articleList'
// 导入频道编辑组件
import ChannelEdit from './components/channelEdit'
// 导入vuex
import { mapState } from 'vuex'
// 导入本地存取方法
import { getItem } from '@/utils/storage'

export default {
  name: 'Home',
  data() {
    return {
      active: 0,
      channels: [],
      editPopupShow: false
    }
  },
  methods: {
    async getChannels() {
      // 考虑在登录或者未登录状态下的不同展示
      // if (this.userToken) {
      //   try {
      //     const { data } = await getUserChannels()
      //     // console.log(data)
      //     this.channels = data.data.channels
      //   } catch (error) {
      //     return this.$toast.fail('获取用户列表失败!')
      //   }
      // } else {
      //   // 未登录,查看本地
      //   const localChannels = getItem('MYCHANNELS')
      //   // 本地有数据,直接使用
      //   if (localChannels) {
      //     this.channels = localChannels
      //   } else {
      //     // 本地无数据,请求默认频道数据展示
      //     try {
      //       const { data } = await getUserChannels()
      //       // console.log(data)
      //       this.channels = data.data.channels
      //     } catch (error) {
      //       return this.$toast.fail('获取用户列表失败!')
      //     }
      //   }
      // }
      // 优化
      try {
        if (this.userToken) {
          const { data } = await getUserChannels()
          this.channels = data.data.channels
        } else {
          // 未登录,查看本地
          const localChannels = getItem('MYCHANNELS')
          // 本地有数据,直接使用
          if (localChannels) {
            this.channels = localChannels
          } else {
            // 本地无数据,请求默认频道数据展示}
            const { data } = await getUserChannels()
            this.channels = data.data.channels
          }
        }
      } catch (error) {
        return this.$toast.fail('获取用户列表失败!')
      }
    },
    changeChannel(index) {
      this.active = index
      this.editPopupShow = false
    },
    changeActive() {
      this.active--
    }
  },
  created() {
    this.getChannels()
  },
  computed: {
    ...mapState(['userToken'])
  },

  // 注册局部组件
  components: {
    articleList: ArticleList,
    channelEditVue: ChannelEdit
  }
}
</script>

<style lang="less" scoped>
.home {
  padding-bottom: 100px;
  padding-top: 92px;
  .searchBtn {
    width: 555px;
    height: 64px;
    font-size: 28px;
    background-color: #5babfb;
    border: none;
  }
  .van-icon {
    font-size: 32px;
  }
}
/deep/.van-tabs__wrap {
  position: fixed;
  z-index: 1;
  left: 0;
  right: 0;
}
.exchange_tab {
  /deep/.van-tabs__content {
    padding-top: 82px;
  }
  /deep/.van-tabs__nav {
    padding: 0 !important;
  }
  /deep/.van-tab {
    box-sizing: border-box;
    min-width: 200px;
    height: 82px;
    color: #777777;
    border: 1px solid #edeff3;
    &.van-tab--active {
      font-size: 30px;
      color: #333333 !important;
    }
  }
  /deep/.van-tabs__line {
    width: 31px !important;
    height: 6px;
    bottom: 8px;
    background-color: #3296fa;
  }
  .placeHolder {
    flex-shrink: 0;
    width: 67px;
    height: 82px;
    border-bottom: 1px solid #edeff3;
  }
  .hamburg {
    position: fixed;
    right: 0;
    width: 67px;
    text-align: center;
    line-height: 82px;
    background: rgba(255, 255, 255, 0.902);
    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      background-image: url('~@/assets/gradient-gray-line.png');
      background-size: contain;
      width: 1px;
      height: 97%;
    }
    .icongengduo {
      font-size: 33px;
    }
  }
}
/deep/.van-icon-cross {
  font-size: 28px;
  color: #222222;
}
</style>
